<?php

class ListingController extends Controller
{
	public $layout = 'awt';
	
	public function actionIndex()
	{
		$this->layout = 'awt';
		$this->pageTitle = 'AWT::团体列表';
		$result = Yii::app()->db->createCommand()
			->select('a.*,count(b.id) as count')
			->from('artist a')
			->leftJoin('album b','a.id=b.artist_id')
			->where('a.artist_status_id=:artist_status_id AND b.album_status_id=:album_status_id', array(':artist_status_id'=>'2',':album_status_id'=>'2'))
			->group('a.id')
			->order('pos asc')
			->queryAll();
		$summary = Yii::app()->db->createCommand()
			->select('sum(en) as en,sum(my) as my,sum(cn) as cn')
			->from('artist a')
			->where('a.artist_status_id=:artist_status_id', array(':artist_status_id'=>'2'))
			->queryRow();
		$albumCount = Yii::app()->db->createCommand()
			->select('count(id) as cnt')
			->from('album a')
			->where('album_status_id=:id', array(':id'=>2))
			->queryRow();
		$songCount = Yii::app()->db->createCommand()
			->select('count(id) as cnt')
			->from('song a')
			#->where('song_status_id=:id', array(':id'=>2))
			->queryRow();
		
		$this->render('index',array(
			'result'=>$result,
			'summary' =>$summary,
			'countAlbum'=>$albumCount['cnt'],
			'countSong'=>$songCount['cnt'],
		));
	}
	public function actionArtist($id)
	{
		$this->layout = 'awt';
		$artist = Yii::app()->db->createCommand()
			->select('*')
			->from('artist a')
			->where('id=:id', array(':id'=>$id))
			->queryRow();
		
		$result = Yii::app()->db->createCommand()
			->select('a.*,count(b.id) as count')
			->from('album a')
			->leftJoin('song b','a.id=b.album_id')
			->where('a.album_status_id=:album_status_id AND a.artist_id=:id', array(':album_status_id'=>'2',':id'=>$id))
			->group('a.id')
			->order('a.year desc')
			->queryAll();
		$albumCount = Yii::app()->db->createCommand()
			->select('count(id) as cnt')
			->from('album a')
			->where('artist_id=:artist_id AND album_status_id=:album_status_id', array(':album_status_id'=>2,':artist_id'=>$id))
			->queryRow();
		
		$tags = array();
		foreach ($result as $row ) 
			if (!empty($row['tags'])) 
				$tags[] = $row['tags'];
		$tags = array_unique($tags);
		
		$this->pageTitle = 'AWT::专辑列表 - '.$artist['name'];
		
		$this->render('artist',array(
			'artist'	=>$artist,
			'result'	=>$result,
			'countAlbum'=>$albumCount['cnt'],
			'tags'		=>$tags,
		));
	}
	
	public function actionAlbum($id) {
		$this->layout = 'awt';
		#album
		$album = Yii::app()->db->createCommand()
			->select('*')
			->from('album a')
			->where('id=:id', array(':id'=>$id))
			->queryRow();
		
		# song
		$result = Yii::app()->db->createCommand()
			->select('a.*')
			->from('song a')
			->where('album_id=:id', array(':id'=>$id))
			->order('track_no asc')
			->queryAll();
					
		# link
		$ids = array();
		foreach ($result as $song) $ids[] = $song['id'];
		$sql = 'SELECT * FROM song_link WHERE song_id IN ('.implode(',',$ids).') OR song_id2 IN('.implode(',',$ids).')';
		$rs = Yii::app()->db->createCommand($sql)->queryAll();
		
		$link1 = array();
		$link2 = array();
		$all   = array();
		foreach ($rs as $key => $row) {
			$all[] = $row['song_id2'];
			$all[] = $row['song_id'];
			if ($row['link_type_id']=='1') {
				foreach ($ids as $id) {
					if ($id == $row['song_id']) $link1[$id][] = $row['song_id2'];
					if ($id == $row['song_id2']) $link1[$id][] = $row['song_id'];
				}
			}
		}
		foreach ($rs as $key => $row) {
			if ($row['link_type_id']=='2') {
				foreach ($ids as $id) {
					if ($id == $row['song_id']) $link2[$id][] = $row['song_id2'];
					if ($id == $row['song_id2']) $link2[$id][] = $row['song_id'];
				}
			}
		}

		$all = array_unique($all);
		$linkinfo = array();
		if (!empty($all)) {
			$sql = 'SELECT a.id,a.name,a.album_id,b.name as album_name FROM song a JOIN album b ON a.album_id=b.id WHERE a.id IN ('.implode(',',$all).')';
			$all = Yii::app()->db->createCommand($sql)->queryAll();
			foreach ($all as $all) {
				$linkinfo[$all['id']] = $all;
			}
		}

		$this->pageTitle = 'AWT::歌曲列表 - '.$album['name'];
		
		$this->render('album',array(
			'album'=>$album,
			'result'=>$result,
			'link1'=>$link1,
			'link2'=>$link2,
			'linkinfo'=>$linkinfo,
		));
	}
	public function actionLyric($id) {
		$result = Yii::app()->db->createCommand()
			->select('*')
			->from('lyric a')
			#->join(' b', 'a.id=b._id')
			->where('song_id=:id', array(':id'=>$id))
			->queryAll();
		
		$this->layout = 'awt-quickview';
		$this->render('lyric',array(
			'result'=>$result,
			'id'=>$id
		));
	}
}
?>